package xin.mydreams.mybill.widget.home.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import xin.mydreams.mybill.util.UserUtil;
import xin.mydreams.mybill.widget.user.entity.User;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * 系统实体对象抽象类<br>
 * 包含主键id，子类不需要定义主键
 * 
 * @Type BaseEntity.java
 * @Desc
 * @mail guzehao@mydreams.xin
 * @author 谷泽昊
 * @date 2019年1月29日 下午9:10:00
 * @version
 */
@Getter
@Setter
@MappedSuperclass
public abstract class UserBaseEntity implements Serializable {
	private static final long serialVersionUID = -2955330485748809219L;

	/**
	 * 对象创建时间
	 */
	@Column(name = "`created_time`", updatable = false)
	@Setter
	@Getter
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createdTime;

	/**
	 * 创建用户
	 */
	@Setter
	@Getter
	@Column(name = "`created_user`", updatable = false)
	private String createdUser;

	/**
	 * 最后修改时间
	 */
	@Setter
	@Getter
	@Column(name = "`last_modified_time`")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date lastModifiedTime;

	/**
	 * 最后修改用户
	 */
	@Setter
	@Getter
	@Column(name = "`last_modified_user`")
	private String lastModifiedUser;

	@PrePersist
	protected void onCreate() {


		createdTime = new Date();
		if (UserUtil.getUser() != null) {
			createdUser = UserUtil.getUser().getUserName();
		}
	}

	@PreUpdate
	protected void onUpdate() {
		lastModifiedTime = new Date();
		if (UserUtil.getUser() != null) {
			User user = UserUtil.getUser();
			if(user!=null){
				lastModifiedUser = UserUtil.getUser().getUserName();
			}

		}
	}
}
